package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import util.Fixtures.Folder;

@Entity(name = "rights")
public class RightEntity  extends DataObjectEntity implements Serializable {

    @OneToMany(cascade = {CascadeType.ALL}, targetEntity=RightLinkEntity.class)
    private Collection<RightLinkEntity> links = new ArrayList<RightLinkEntity>();

    private String rightName;
    
    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER,
    targetEntity = PersonEntity.class, mappedBy = "rights")
    private Collection<PersonEntity> users = new ArrayList<PersonEntity>();
    
    @ManyToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER,
    targetEntity = PersonEntity.class, mappedBy = "rights")
    private Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
    private Folder folder;

    public Folder getFolder() {
        return folder;
    }

    public void setFolder(Folder folder) {
        this.folder = folder;
    }
    public RightEntity() {
    }

    public Collection<RightLinkEntity> getLinks() {
        return links;
    }

    public void setLinks(Collection<RightLinkEntity> links) {
        this.links = links;
    }
    
    
    public RightEntity(String name) {
    rightName = name;
    }


    public String getRightName() {
        return rightName;
    }

    public void setRightName(String rightName) {
        this.rightName = rightName;
    }

    public Collection<RoleEntity> getRoles() {
        return roles;
    }

    public void setRoles(Collection<RoleEntity> roles) {
        this.roles = roles;
    }

    public Collection<PersonEntity> getUsers() {
        return users;
    }

    public void setUsers(Collection<PersonEntity> users) {
        this.users = users;
    }
          @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.getRightName() != null ? this.getRightName().hashCode()
                : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

// TODO: Warning - this method won't work in the case the groupNamefields are not set
        if (!(object instanceof RightEntity)) {
            return false;
        }
        RightEntity other = (RightEntity) object;
        if (this.getRightName() != other.getRightName()
                && (this.getRightName() == null
                || !this.getRightName().equals(other.getRightName()))) {
            return false;
        }
        return true;
    }

    @Override
    public String childToString() {
        return "RightEntity{" + "links=" + links + ", rightName=" + rightName + ", users=" + users + ", roles=" + roles + '}';
    }

  
  @Override
    public String toString() {
        return "entity.Group[id="
                + getRightName() + "]";
    }
}